//
//  HomeView.m
//  TourPlay
//
//  Created by dllo on 15/11/16.
//  Copyright © 2015年 王军岐. All rights reserved.
//

#import "HomeView.h"

@interface HomeView ()

@property (nonatomic, retain) UIButton *addBtn; // 地址按钮
@property (nonatomic, retain) UIButton *toDayBtn; // 今天按钮
@property (nonatomic, retain) UIButton *foundBtn;
@property (nonatomic, retain) UIButton *recordBtn;
@property (nonatomic, retain) UIImageView *imageV; // 三角形

@end



@implementation HomeView
- (void)dealloc
{
    [_addBtn release];
    [_toDayBtn release];
    [_foundBtn release];
    [_recordBtn release];
    [_imageV release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
    }
    return self;
}
- (void)createSubViews
{
    NSString *cityName = [[NSUserDefaults standardUserDefaults] objectForKey:@"cityName"];
    
    self.addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    self.addBtn.backgroundColor = [UIColor orangeColor];
    
    [self.addBtn setTitle:cityName forState:UIControlStateNormal];
//    self.addBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
    //右对齐
    [self.addBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    self.addBtn.titleLabel.font = NAVG_HOME_FONT;
    self.addBtn.frame = CGRectMake(0, 0, [AppTools widthForContent:cityName] + 10, self.frame.size.height);
//    [self.addBtn setTitleColor:NAVG_FONTCOLOR_ON forState:UIControlStateNormal];
    [self.addBtn addTarget:self action:@selector(addBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.addBtn];
    
    self.imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"map_drop_down_button_coffee@2x.png"]];
    self.imageV.userInteractionEnabled = YES;
    self.imageV.frame = CGRectMake(self.addBtn.frame.origin.x + self.addBtn.frame.size.width + 5, 22 - 4.5, 12, 9);
    [self addSubview:self.imageV];
    [_imageV release];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [self.imageV addGestureRecognizer:tap];
    [tap release];

    // 今天按钮
    self.toDayBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.toDayBtn.frame = CGRectMake(self.imageV.frame.origin.x + self.imageV.frame.size.width, self.addBtn.frame.origin.y, 50, self.addBtn.frame.size.height);
    self.toDayBtn.titleLabel.font = NAVG_HOME_FONT;
    [self.toDayBtn setTitle:@"今天" forState:UIControlStateNormal];
    [self.toDayBtn setTitleColor:NAVG_FONTCOLOR_OFF forState:UIControlStateNormal];
    [self.toDayBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.toDayBtn.tag = 1000;
//    self.toDayBtn.backgroundColor = [UIColor cyanColor];
    //右对齐
    [self.toDayBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [self addSubview:self.toDayBtn];
    
    // 发现按钮
    self.foundBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.foundBtn.frame = CGRectMake(self.toDayBtn.frame.origin.x + self.toDayBtn.frame.size.width, self.addBtn.frame.origin.y, self.toDayBtn.frame.size.width, self.toDayBtn.frame.size.height);
//    self.foundBtn.backgroundColor = [UIColor orangeColor];
    self.foundBtn.titleLabel.font = NAVG_HOME_FONT;
    [self.foundBtn setTitleColor:[ColorSingleton shareSingleton].myNavigationBarTextColor forState:UIControlStateNormal];
    [self.foundBtn setTitle:@"发现" forState:UIControlStateNormal];


    //右对齐
    [self.foundBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [self.foundBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.foundBtn.tag = 1001;
    [self addSubview:self.foundBtn];
    
    // 记录按钮
    self.recordBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.recordBtn.frame = CGRectMake(self.foundBtn.frame.origin.x + self.foundBtn.frame.size.width, self.foundBtn.frame.origin.y, self.foundBtn.frame.size.width, self.foundBtn.frame.size.height);
    //    self.foundBtn.backgroundColor = [UIColor orangeColor];
    self.recordBtn.titleLabel.font = NAVG_HOME_FONT;
    [self.recordBtn setTitle:@"记录" forState:UIControlStateNormal];
    [self.recordBtn setTitleColor:NAVG_FONTCOLOR_OFF forState:UIControlStateNormal];
    //右对齐
    [self.recordBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [self.recordBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.recordBtn.tag = 1002;
    [self addSubview:self.recordBtn];
    
    
    
    [self addColorChangedBlock:^{
        
        self.foundBtn.normalTitleColor = [ColorSingleton shareSingleton].myNavigationBarTextColor;
        self.foundBtn.nightTitleColor = [ColorSingleton shareSingleton].myNavigationBarTextColor;
        
        self.addBtn.normalTitleColor = [ColorSingleton shareSingleton].myNavigationBarTextColor;
        self.addBtn.nightTitleColor = [ColorSingleton shareSingleton].myNavigationBarTextColor;
        
    }];
    
}
- (void)tapAction
{
    [self.delegate addBtnToAddressViews];
}
- (void)buttonAction:(UIButton *)sender
{
    [self.delegate changeScrollViewWithTag:sender.tag];
    [self changeButtonColorWithTag:sender.tag];
}

- (void)changeButtonColorWithTag:(NSInteger)tag
{
    UIButton *button = (UIButton *)[self viewWithTag:tag];
//    [button setTitleColor:NAVG_FONTCOLOR_ON forState:UIControlStateNormal];
    
    [self addColorChangedBlock:^{
        
        button.normalTitleColor = NAVG_FONTCOLOR_ON;
        button.nightTitleColor = [UIColor whiteColor];

    if (self.toDayBtn.tag == tag) {
        [self.foundBtn setTitleColor:NAVG_FONTCOLOR_OFF forState:UIControlStateNormal];
        [self.recordBtn setTitleColor:NAVG_FONTCOLOR_OFF forState:UIControlStateNormal];
        
    } else if (self.foundBtn.tag == tag) {
        [self.toDayBtn setTitleColor:NAVG_FONTCOLOR_OFF forState:UIControlStateNormal];
        [self.recordBtn setTitleColor:NAVG_FONTCOLOR_OFF forState:UIControlStateNormal];
    } else {
        [self.toDayBtn setTitleColor:NAVG_FONTCOLOR_OFF forState:UIControlStateNormal];
        [self.foundBtn setTitleColor:NAVG_FONTCOLOR_OFF forState:UIControlStateNormal];
    }
        
        }];
    
}
- (void)addBtnAction
{
    [self.delegate addBtnToAddressViews];
}
//#pragma mark - 适配横向宽度
//- (CGFloat)widthForContent:(NSString *)content
//{
//    CGSize size = CGSizeMake(1000, 44);
//    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:NAVG_HOME_FONT, NSFontAttributeName, nil];
//    CGRect frame = [content boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
//    return frame.size.width;
//}
- (void)changeCity
{
    NSString *cityName = [[NSUserDefaults standardUserDefaults] objectForKey:@"cityName"];
    NSLog(@"%@ %@", cityName, [[NSUserDefaults standardUserDefaults] objectForKey:@"cityid"])
    [self.addBtn setTitle:cityName forState:UIControlStateNormal];
    // 当城市改变时, 改变坐标
    self.addBtn.frame = CGRectMake(0, 0, [AppTools widthForContent:cityName] + 10, self.frame.size.height);
    self.imageV.frame = CGRectMake(self.addBtn.frame.origin.x + self.addBtn.frame.size.width + 5, 22 - 4.5, 12, 9);
    self.toDayBtn.frame = CGRectMake(self.imageV.frame.origin.x + self.imageV.frame.size.width, self.addBtn.frame.origin.y, 50, self.addBtn.frame.size.height);
    self.foundBtn.frame = CGRectMake(self.toDayBtn.frame.origin.x + self.toDayBtn.frame.size.width, self.addBtn.frame.origin.y, self.toDayBtn.frame.size.width, self.toDayBtn.frame.size.height);
    self.recordBtn.frame = CGRectMake(self.foundBtn.frame.origin.x + self.foundBtn.frame.size.width, self.foundBtn.frame.origin.y, self.foundBtn.frame.size.width, self.foundBtn.frame.size.height);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
